package com.example.demo.serivce.impl.message.notify;

import com.example.demo.core.util.SipUtil;
import gov.nist.javax.sip.RequestEventExt;
import gov.nist.javax.sip.message.SIPResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.sip.PeerUnavailableException;
import javax.sip.RequestEvent;
import javax.sip.SipFactory;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.ExpiresHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.ToHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;
import java.text.ParseException;

@Service
@Slf4j
public class KeepaliveRequestHandler {


@Resource
SipFactory sipFactory;

    public Response Keepalive(RequestEvent requestEvent) throws ParseException, PeerUnavailableException{
        RequestEventExt evtExt = (RequestEventExt) requestEvent;
        Request request = requestEvent.getRequest();
        String deviceId = SipUtil.getDeviceId(request);
        MessageFactory messageFactory = sipFactory.createMessageFactory();
        log.info("[收到心跳保活],[地址] {},[端口] {} [设备id],{}", evtExt.getRemoteIpAddress(), evtExt.getRemotePort(), deviceId);
        return messageFactory.createResponse(Response.OK, request);
     }
}
